<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="/WEB-INF/tld/c.tld"  %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE html>
<html>
	<head>
		<base href="<%=basePath%>">
		<title>用户管理</title>
		<%@include file="../common/common_ui.jsp" %>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<style type="text/css">
			.selected{
				background-color: #efefef;
			}
		</style>
	</head>

	<body style="border: 0;margin: 0;padding: 0;">
	<div>		
		<input type="hidden" id="cur_userId">	
		<!-- 右边角色管理start -->
		<div id="roleShow" style="float: right;width:26%;height:710px" >
			<div class="widget" style="overflow:auto;height:100%;max-height:705px;">
				<div class="widget-head">
					<div class="pull-left">角色</div>
					<div class="pull-right">
						<button id="btn_distribution" data-loading-text="loading" class="btn btn-xs btn-primary">分配角色</button>
					</div>
					<div class="clearfix"></div>
				</div>
				<div id="dis_msg"></div>
				<div class="widget-content">
					<table id="roleTable" class="table table-striped table-bordered table-hover">
						<c:forEach var="info" items="${roles}">
							<tr>
								<td width="10%" valign="middle">
								<input name="chkrole" type="checkbox" id="ck_role_${info.id}" value="${info.id}" >
								</td>
								<td valign="middle" style="padding:0">
									<label for="ck_role_${info.id}" style="padding:10px;display: block;">${info.name}</label>
								</td>
							</tr>	
						</c:forEach>
					</table>
				</div>
			</div>
		</div>
		<!-- 右边角色管理 end-->
		<div style="float: left;width:73%">
			<div class="widget">
				<div class="widget-head">
					<div class="pull-left">
						查询条件
					</div>
				<div class="clearfix"></div>
			</div>
			<div class="widget-content">
				<div class="padd">
					<!-- Form starts.  -->
					<form id="frm" name="frm" class="form-horizontal" role="form" method="post" action="userInfo/queryUser.do">
						<table>
							<tr>
								<td><label>账号:</label>&nbsp;&nbsp;</td>
								<td>
									<input id="userName" name="userName" type="text" class="form-control" value="${param.userName}">
								</td>
								<td>&nbsp;&nbsp;<label>姓名:</label>&nbsp;&nbsp;</td>
								<td>
									<input id="realName" name="realName" type="text" class="form-control" value="${param.realName}">
								</td>
								<td>
									&nbsp;&nbsp;
									<!-- Split button -->
									<div class="btn-group">
									  <button type="submit" id="search" class="btn btn-primary">查询</button>
									  <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
									    <span class="caret"></span>
									    <span class="sr-only">Toggle Dropdown</span>
									  </button>
									  <ul class="dropdown-menu" role="menu">
									    <li><a href="javascript:$('#status').val(1);document.frm.submit();">查询有效用户</a></li>
									    <li><a href="javascript:$('#status').val(0);document.frm.submit();">查询无效用户</a></li>
									  </ul>
									</div>
									&nbsp;&nbsp;
									<button type="button" id="btnAdd" style="margin-left:2px" class="btn btn-default">新增</button>
									<!-- 状态1，有效；0，无效 -->
									<input type="hidden" name="status" id="status" value="${status}">									
								</td>
							</tr>
							
						</table>
					</form>
				</div>
			</div>
		</div>
		
		<div class="widget">
			<div class="widget-head">
				<div class="pull-left">
					查询结果
				</div>
				<div class="clearfix"></div>
			</div>

			<div class="widget-content">
				<table class="table table-hover table-bordered">
					<thead>
						<tr>
							<th>序号</th>
							<th>用户名</th>
							<th>姓名</th>
							<th>状态</th>
							<th>操作</th>
						</tr>
					</thead>
					<tbody id="userInfos">
						<c:forEach items="${pageResult.list}" var="info" varStatus="i">
							<tr class="cktr" myid="${info.id}" mystatus="${info.status }">
								<td>${i.index+1+pageResult.pageSize*(pageResult.current-1)}</td>
								<td>${info.userName}</td>
								<td>${info.realName}</td>
								<td>
									<c:choose>
										<c:when test="${info.status eq '1'}"><span class="label label-success">有效</span></c:when>
										<c:otherwise>
											无效
										</c:otherwise>
									</c:choose>
								</td>
								<td>
								<c:if test="${info.status=='1'}">
											<button type="button" class="btn btn-xs btn-info"
												id="btnModify" onclick="updateUser('${info.id}');">
												<span class="glyphicon glyphicon-pencil"></span>编辑
											</button>&nbsp;
							<button type="button" class="btn btn-xs btn-danger" id="btnDel"
												value="作废"
												onclick="deleteUser('${info.id}');">
												<span class="glyphicon  glyphicon-trash"></span>作废
											</button>&nbsp;
											<button type="button" class="btn btn-xs btn-info" id="btnReset"
												value="重置密码"
												onclick="resetPwd('${info.id }','${info.userName}')">
												<span class="glyphicon glyphicon-th-list"></span>重置密码
											</button>&nbsp;
							</c:if> <c:if test="${info.status=='0'}">
											<button type="button" class="btn btn-xs btn-success"
												id="btnDel"
												onclick="restartUser('${info.id}');">
												<span class="glyphicon glyphicon-ok"></span>启用
											</button>
										</c:if>
									</td>
							</tr>
							<tr>
							<input type="hidden" value="${info.id}" name="id" id="id"> </tr>
						</c:forEach>
					</tbody>
				</table>
				<fw:page formName="pageResult" />
			</div>
		</div>
		</div>
	</div>
<script type="text/javascript">
$(document).ready(function(){	
	//分配角色
	$("#btn_distribution").click(function(){
		var userId=$("#cur_userId").val();
		if(userId==''){
			$.fwAlert("dis_msg","请先选择账号");
			return;
		}
		window.parent.sconfirm("确认要分配角色吗？",function (){
			$("#btn_distribution").button('loading');
  			var roleIds="";
			$(":checkbox[name =chkrole]:checked").each(function (){
				var _this=$(this);
				roleIds+=_this.val()+",";
			});
			$.ajax({
				url:'<%=path %>/userInfo/updateUserRole.do',
				data:{userId:userId,roleIds:roleIds},
				type: "post",
	   			dataType: "text",
				success:function(data){					
		 			if(data=='Y'){
		 				$.fwAlert("dis_msg","分配成功","success");
		 			}
				},complete:function (){
					$("#btn_distribution").button('reset');
				}
			});
  		}); 
		
	});
	
	//新增
	$("#btnAdd").click(function (){	
		window.parent.sopen("<%=path%>/userInfo/toEditPage.do","新增用户",400,250);
	});
	$(".cktr").click(function (){
		var _this=$(this);
		$(".selected").removeClass("selected")
		_this.addClass("selected");		
		var id=_this.attr("myid");
		var status=_this.attr("mystatus");
		if(status==0){
			$("#btn_distribution").attr("disabled",true);
		}else{
			$("#btn_distribution").attr("disabled",false);
		}
		getUserRoleInfo(id);		
	});
 });    	  
  	
  	//加载用户角色
  	function getUserRoleInfo(uid){
  	 	$("#cur_userId").val(uid)
  	 	$("[name =chkrole]:checkbox").prop("checked", false);
  	 	$.ajax({
  			url:'<%=path %>/userInfo/getUserRoleInfo.do',
  			data:{userId:uid},
  			type: "post",
	    	dataType: "json",
  			success:function(data){	   				
  	 			for(var i in data){    	 				
  	 				var id=data[i].roleId;
  	 				$("#ck_role_"+id).prop("checked",true);
  	 			}	   				
  			}
  		});	   		
	}
  	
  	//to编辑
	function updateUser(id){
		window.parent.sopen("<%=path%>/userInfo/toEditPage.do?method=update&id="+id,"修改用户",400,250);
	}

	/*
	* 作废
	*/
	function deleteUser(id){
		 var message = "确定作废";
		 window.parent.sconfirm(message, function(){
			 $.post("<%=path%>/userInfo/deleteUser.do", {
	             times:new Date().getTime(),
	             id:id
	             }, function(data) {
	            $("#frm").submit();
	     	});
		 });
	};
	
	/*
	* 重启
	*/
	function restartUser(id){
		 var message = "确定启用";
		 window.parent.sconfirm(message, function(){
			 $.post("<%=path%>/userInfo/restartUser.do", {
	             times:new Date().getTime(),
	             id:id
	             }, function(data) {
	            $("#frm").submit();
	     	});
		 });	 
	};
	/*
	* 重置密码
	*/
	function resetPwd(id,userName){
		 var message = "确定重置密码";
		 window.parent.sconfirm(message, function(){
			 $.post("<%=path%>/userInfo/resetPwd.do", {
	             	times:new Date().getTime(),
	             	id:id	             		             
	             }, function(data) {
	         		alert('密码修改成功')
			 });
		 });
	};

</script>
	</body>
</html>
